﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Model.Context;
using Model.Entity;
using Ones.Dal.Repository.Impl;

namespace Ones.Wcf.Editorial
{
    public class ServiceNewsManagement : IServiceNewsManagement
    {
        private OnlineNewsEditorialDataContext db;
        private INewsDao indao;
        private INewsVersionDao invdao;

        #region Constructors
        public ServiceNewsManagement()
        {
            db = new OnlineNewsEditorialDataContext();
            indao = new NewsDao(db);
            invdao = new NewsVersionDao(db);
        }
        #endregion

        #region DMM

        /// <summary>
        /// Insert data into the News and NewsVersions tables
        /// </summary>
        /// <param name="news"></param>
        /// <param name="newsversions"></param>
        /// <returns>bool</returns>
        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool ComposeNews(News news, NewsVersion newsversions)
        {
            try
            {
                //news
                bool res1 = indao.create(news);
                newsversions.newsId = news.newsId;

                //newsVersion
                bool res2 = invdao.create(newsversions);

                if (res1 && res2)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("Unexpected error found: {0}", ex));
                return false;
            }
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool UpdateNews(News news, NewsVersion newsversions)
        {
            try
            {
                //news
                bool res1 = indao.update(news);
                newsversions.newsId = news.newsId;
                //newsVersion
                bool res2 = invdao.create(newsversions);

                if (res1 && res2)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("Unexpected error found: {0}", ex));
                return false;
            }
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool DeleteNews(int newsId)
        {
            try
            {
                //newsVersion
                bool res2 = invdao.delete(newsId);

                //news
                bool res1 = indao.delete(newsId);

                if (res1 && res2)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("Unexpected error found: {0}", ex));
                return false;
            }
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool RemoveNews(int newsId)
        {
            try
            {
                //news
                bool res = indao.remove(newsId);

                if (res)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("Unexpected error found: {0}", ex));
                return false;
            }
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public bool ReviseNews(string type, News n, NewsVersion nv)
        {
            try
            {
                var currentNews = indao.getById(n.newsId);

                switch (type)
                {
                    case "RejectE":
                        currentNews.processStatus = "RejectE";
                        nv.verProcessDirection = "Reject";
                        break;
                    case "RejectA":
                        currentNews.processStatus = "RejectA";
                        nv.verProcessDirection = "Reject";
                        break;
                    case "Approve":
                        currentNews.processStatus="Approve";
                        currentNews.verified = true;
                        currentNews.publishDate = System.DateTime.Now;
                        nv.verProcessDirection = "Post";
                        break;
                    default:
                        break;
                }

                bool res1 = indao.update(currentNews);

                nv.newsId = currentNews.newsId;
                
                bool res2 = invdao.create(nv);
                if (res1 && res2)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("Unexpected error found: {0}", ex));
                return false;
            }
        }

        #endregion

        #region DQM

        /// <summary>
        /// Retrieve all news filtered by news exclusion from database
        /// </summary>
        /// <returns></returns>
        public List<viewNews> getAllFilterNews(bool availableNews)
        {
            return indao.getAllAvailableNews(availableNews);
        }

        /// <summary>
        /// Retrieve news based on news id search
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public News RetrieveNewsByID(int newsId)
        {
            return indao.getById(newsId);
        }

        /// <summary>
        /// List all data in the News table
        /// </summary>
        /// <returns>none</returns>

        /// <summary>
        /// Retrieve News based on Category ID search
        /// </summary>
        /// <param name="newsTitle"></param>
        /// <returns>list of news having same category id</returns>
        public List<News> RetrieveNewsByTitle(string newsTitle)
        {
            return indao.getByTitle(newsTitle);
        }

        /// <summary>
        /// Retrieve list of news based on news title relative search
        /// </summary>
        /// <param name="catID"></param>
        /// <returns></returns>
        public List<News> RetrieveNewsByCategoryID(string catId)
        {
            return indao.getByCategory(catId);
        }


        /// <summary>
        /// Retrieve list of news based on employee id search
        /// </summary>
        /// <param name="empId"></param>
        /// <returns></returns>
        public List<News> RetrieveNewsByEmployeeID(string empId)
        {
            return indao.getByAuthor(empId);
        }

        #endregion
    }
}
